<?php

namespace App\Http\Controllers\Api;

use App\Models\Role;
use App\Models\User;
use App\Transformers\UserTransformer;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    public function me()
    {
        $user = $this->user();
        $roles = [];
        if ($user->name === 'admin') {
            $roles = ['admin'];
        } else {
            $rs = $user->roles;
            foreach ($rs as $v) {
                $roles[] = Role::ROLES_NAME[(string) $v->role_id];
            }
        }
        return $this->response->item($this->user(), new UserTransformer())
            ->setMeta(['roles' => $roles]);
    }

    public function searchByName(Request $request)
    {
        $name = $request->input('name');

        $users = User::where('name', 'LIKE', '%'.$name.'%')->get();

        return $this->response->collection($users, new UserTransformer());
    }
}
